package com.manda.smart.hub.common.map;

import cn.hutool.core.getter.OptNullBasicTypeFromObjectGetter;

import java.util.Map;
import java.util.function.Function;

/**
 * 将任意具有映射关系的数据结构转为类似 StringMap 的结构
 *
 * @author hongda.li
 */
public class StringMapping implements OptNullBasicTypeFromObjectGetter<String> {

    private final Function<String, Object> mapping;

    public StringMapping(Map<String, Object> map) {
        this(map::get);
    }

    public StringMapping(Function<String, Object> mapping) {
        this.mapping = mapping;
    }

    @Override
    public Object getObj(String key, Object defaultValue) {
        Object apply = this.mapping.apply(key);
        return apply == null ? defaultValue : apply;
    }
}
